package com.example.demo.model;

import com.example.demo.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ReturnResult<T> {
    private ResultCode code;
    private String errorMsg;
    private T data;

    public static <T> ReturnResult<T> success(T data){
        ReturnResult<T> returnResult = new ReturnResult<>();
        returnResult.setCode(ResultCode.SUCCESS);
        returnResult.setErrorMsg("");
        returnResult.setData(data);
        return returnResult;
    }

    public static <T> ReturnResult<T> failure(String errorMsg, T data){
        ReturnResult<T> returnResult = new ReturnResult<>();
        returnResult.setCode(ResultCode.FAILURE);
        returnResult.setErrorMsg(errorMsg);
        returnResult.setData(null);
        return returnResult;
    }

    public static <T> ReturnResult<T> notLoggedIn(){
        ReturnResult<T> returnResult = new ReturnResult<>();
        returnResult.setCode(ResultCode.NOT_LOGGED_IN);
        returnResult.setErrorMsg("未登录");
        returnResult.setData(null);
        return returnResult;
    }

    public static <T> ReturnResult<T> error(String errorMsg){
        ReturnResult<T> returnResult = new ReturnResult<>();
        returnResult.setCode(ResultCode.ERROR);
        returnResult.setErrorMsg(errorMsg);
        returnResult.setData(null);
        return returnResult;
    }
}
